package com.blog.service.user.impl;

import com.blog.constant.ErrorConstant;
import com.blog.exception.BusinessException;
import com.blog.mapper.UserMapper;
import com.blog.model.User;
import com.blog.service.user.UserService;
import com.blog.utils.MethodUtil;
import com.blog.utils.TaleUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

/**
 * User的业务接口实现类
 *
 * @author wmb
 *
 * @since 2021-2-7
 */
@Service("userService")
public class UserServiceImpl implements UserService {

    @Autowired
    @Qualifier("userMapper")
    private UserMapper userMapper;

    /**
     * 用户登录
     *
     * @param username 用户名
     * @param password 密码
     *
     * @return
     */
    @Override
    public User login(String username, String password) {
        // MD5加密
        String pwd = TaleUtils.MD5encode(username + password);
        User user = userMapper.getUserInfoByCond(username, pwd);
        if (null == user) {
            throw new BusinessException(ErrorConstant.USERNAME_PASSWORD_ERROR, MethodUtil.getLineInfo());
        }
        return user;
    }

    /**
     * 根据主键编号获取用户信息
     *
     * @param uid   主键
     *
     * @return
     */
    @Override
    public User getUserInfoById(Integer uid) {
        return userMapper.getUserInfoById(uid);
    }

    /**
     * 更改用户信息
     *
     * @param user
     *
     * @return
     */
    @Override
    public int updateUserInfo(User user) {
        if (user.getUid() == null) {
            throw new BusinessException(ErrorConstant.USER_ID_IS_NOT_NULL, MethodUtil.getLineInfo());
        }

        return userMapper.updateUserInfo(user);
    }
}
